package GUI;



import java.awt.Font;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.ArrayList;

import javax.naming.spi.DirStateFactory.Result;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import client.ClientConsole;

import Library.ReaderCard;
/**
 * @author mishel hen
 *
 */
/**
 * This class is the edit reader card
 */
public class GUI_EditReaderCard extends JPanel
{
	/**
	 * client Connected
	 */
	private ClientConsole cc;
	/**
	 * The student's details
	 */
	private  ReaderCard readercard;
	/**
	 * Text field of the student's name
	 */
	private JTextField textName;
	/**
	 * Text field of the student's last name  
	 */
	private JTextField textLastName;
	/**
	 * Text field of the student's phone  
	 */
	private JTextField textPhone;
	/**
	 * Text field of the student's email  
	 */
	private JTextField textEmail;
	
	/**
	 * The student can edit the personal information
	 * @param clc
	 * @param readercard2
	 * @param drc
	 */
	public GUI_EditReaderCard(ClientConsole clc, ReaderCard readercard2, final GUI_DisplayReaderCard drc)
	{
		super();	
		cc = clc;
		readercard = readercard2;
		this.setSize(600, 400);
		JButton btnEdit = new JButton("Submit");
		btnEdit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				String firstName=textName.getText();
				String phone=textPhone.getText();
				String email=textEmail.getText();
				String lastName=textLastName.getText();
				if ((!firstName.equals(readercard.getFirstName())) || (!phone.equals(readercard.getPhone())) || (!lastName.equals(readercard.getLastName())) || (!email.equals(readercard.getEmail())))
				{
					String s[]={"editPersonalDetails",(readercard.getReaderId()).toString(),firstName,phone,email,lastName};
				try {
					cc.client.sendToServer(s);

				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

				while(cc.client.flag==0)
				    System.out.print("");
				 cc.client.flag=0;

				String succ = null;
				boolean msg = (Boolean)cc.client.message;
				if (msg)
					succ = "The details change succesfuly";
				else
					succ = "Error while change the details";
				JOptionPane.showMessageDialog(new JFrame(),succ);
			}
				else
					JOptionPane.showMessageDialog(new JFrame(),"No details were change");
					
		}});
		
	
	setLayout(null);
	btnEdit.setBounds(60, 320, 110, 30);
	this.add(btnEdit);
	this.setVisible(true);	
		JLabel lblTitle = new JLabel("Edit details ");
		lblTitle.setFont(new Font("Tahoma", Font.PLAIN, 24));

		lblTitle.setBounds(230, 20, 200, 40);
		add(lblTitle);
		
		JLabel lblUsername = new JLabel("User name: " + cc.userName );
		lblUsername.setBounds(60, 70, 200, 20);
		add(lblUsername);
		
		JLabel lblPhone = new JLabel("Phone: ");
		lblPhone.setBounds(60, 110, 200, 20);
		add(lblPhone);
		
		textPhone = new JTextField();
		textPhone.setBounds(160, 110, 150, 20);
		textPhone.setText(readercard.getPhone());
		add(textPhone);
		
		JLabel lblEmail = new JLabel("Email: ");
		lblEmail.setBounds(60, 150, 200, 20);
		add(lblEmail);
		
		textEmail = new JTextField();
		textEmail.setBounds(160, 150, 150, 20);
		textEmail.setText(readercard.getEmail());
		add(textEmail);
		
		JLabel lblFirstName = new JLabel("First Name: ");
		lblFirstName.setBounds(60, 190, 200, 20);
		add(lblFirstName);
		
		textName = new JTextField();
		textName.setBounds(160, 190, 150, 20);
		textName.setText(readercard.getFirstName());
		add(textName);
		
		JLabel lblLastName = new JLabel("Last Name: " );
		lblLastName.setBounds(60, 230, 200, 20);
		add(lblLastName);
		
		textLastName = new JTextField();
		textLastName.setBounds(160, 230, 150, 20);
		textLastName.setText(readercard.getLastName());
		add(textLastName);
		
		JLabel lblStatus = new JLabel("Status: " + readercard.getStatus());
		lblStatus.setBounds(60, 270, 200, 20);
		add(lblStatus);

		JButton btnBack = new JButton("Back");
		btnBack.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				cc.cgui.setContentPane(drc);
			}
		})
		;
		btnBack.setBounds(200, 320, 110, 30);
		add(btnBack);
		


	}

}
